package com.shiwaixiangcun.customer.adapter;

import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.entity.Recommend;
import com.shiwaixiangcun.customer.glide.ImageDisplayUtil;
import com.shiwaixiangcun.customer.utils.DisplayUtil;

import java.util.List;

/**
 * Author   Administrator
 * Date     2018/1/18
 * Desc     eg
 *
 * @author Administrator
 */

public class AdapterRecommend extends BaseQuickAdapter<Recommend, BaseViewHolder> {
    public AdapterRecommend(@Nullable List<Recommend> data) {
        super(R.layout.item_recommend, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Recommend item) {

        //动态测量item数据
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        int viewWith = (width - (DisplayUtil.INSTANCE.dip2px(mContext, 28f) * 2)) / 2;
        int viewHeight = viewWith / 2;
        int position = helper.getAdapterPosition();


        boolean isLeft = false;
        isLeft = position % 2 == 0;
        ImageView imageView = helper.getView(R.id.image);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        if (isLeft) {
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        } else {
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);

        }
        layoutParams.width = viewWith;
        layoutParams.height = viewHeight;
        imageView.setLayoutParams(layoutParams);


        ImageDisplayUtil.showImageView(mContext, item.getImagePath(), imageView);


    }


    public void clearData() {

        mData.clear();
        notifyDataSetChanged();
    }
}
